package cn.devzyh.service.impl;

import cn.devzyh.dao.MenuMapper;
import cn.devzyh.domain.Menu;
import cn.devzyh.domain.MenuVo;
import cn.devzyh.service.MenuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单模块业务层
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> findByPid(Integer parentId) {

        return menuMapper.findByPid(parentId);
    }

    @Override
    public PageInfo<Menu> findAllMenu(MenuVo menuVo) {

        PageHelper.startPage(menuVo.getCurrentPage(), menuVo.getPageSize());
        List<Menu> menus = menuMapper.findAll();
        PageInfo<Menu> pageInfo = new PageInfo<>(menus);

        return pageInfo;
    }

    @Override
    public Map<String, Object> findMenuInfoById(Integer id) {

        Map<String, Object> map = new HashMap<>();

        Menu menu = menuMapper.findById(id);
        map.put("menuInfo", menu);

        List<Menu> menus = menuMapper.findByPid(menu.getParentId());
        map.put("parentMenuList", menus);

        return map;
    }

    @Override
    public void saveMenu(Menu menu) {

        Date now = new Date();
        menu.setCreatedTime(now);
        menu.setUpdatedTime(now);
        menu.setCreatedBy("system");
        menu.setUpdatedBy("system");

        menuMapper.saveMenu(menu);
    }

    @Override
    public void updateMenu(Menu menu) {

        menu.setUpdatedTime(new Date());
        menu.setUpdatedBy("system");

        menuMapper.updateMenu(menu);
    }
}
